package com.sf.osms.dep.processor.builder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sf.osms.dep.domain.Agent;
import com.sf.osms.dep.domain.AgentConfig;
import com.sf.osms.dep.domain.AgentFileMap;
import com.sf.osms.dep.processor.EventProcessor;
import com.sf.osms.dep.processor.exception.RouterException;
import com.sf.osms.dep.processor.handler.EventHandler;
import com.sf.osms.dep.processor.model.EventModel;
import com.sf.osms.dep.processor.parser.EventParser;
import com.sf.osms.dep.processor.util.ExceptionUtils;

public class FileProcessor<T,E> {
	
	private final static Logger logger = Logger.getLogger(EventProcessor.class);

	private EventParser<T, E> parser;
	private EventHandler<T, E> handler;
	
	
	public FileProcessor(EventParser<T, E> parser,
			EventHandler<T, E> handler){
		this.parser = parser;
		this.handler = handler;
	}
	
	public List<E> processAgentEvent(File file,Agent agentInfo){
		
		//必要配置信息的前置检查
		if(null == agentInfo|| null == agentInfo.getAgentFileMap()){
			
			logger.error("*******代理：" + agentInfo.getName() + "的必要的配置信息不全！");
			
			return null;
		}
		
			EventModel<T, E> eventModel = null;
			try {
			    
				//解析文件为初始数据
				logger.info("******代理：" + agentInfo.getName() + "开始解析文件:" + file.getAbsolutePath());
				eventModel = parser.parse(file, agentInfo);
				
			} catch (RouterException e) {
				logger.error("******代理：" + agentInfo.getName() + "   :解析" + file.getAbsolutePath()+"出现异常！******"+e);
				logger.error(ExceptionUtils.getExceptionStackString(e));
				return null;
			}
			
			try {
				//处理初始数据为DB记录
				logger.info("******代理：" + agentInfo.getName() + "开始处理文件:" + file.getAbsolutePath());
				eventModel = handler.handle(eventModel, agentInfo);
			} catch (Exception e) {
				logger.error("******代理：" + agentInfo.getName() + "   :处理" + file.getAbsolutePath()+"出现异常！******"+e);
				logger.error(ExceptionUtils.getExceptionStackString(e));
			
				return null;
			}
			
			return eventModel.getCorrectRouter();
			
		}
	
	
	public static void main(String[] args)  throws Exception{
		File checkFile = new File("E:/tt.xlsx");
		String checkRules = "_1,1!_2,2";
		List<AgentConfig> agentConfigs = new ArrayList<AgentConfig>();
		AgentConfig a1 = new AgentConfig();
		a1.setAgentCode("if(router.gt().get(\"_1\") != null)");
		a1.setSfCode("31");
		a1.setConfigType(2);
		agentConfigs.add(a1);
		AgentConfig a2 = new AgentConfig();
		a2.setAgentCode("if(router.gt().get(\"_3\") != null)");
		a2.setSfCode("32");
		a2.setConfigType(2);
		agentConfigs.add(a2);
		Agent agentInfo = new Agent();
		AgentFileMap agentFileMap = new AgentFileMap();
		agentFileMap.setDataLine(2);
		
		String suffix = checkFile.getName().substring(checkFile.getName().indexOf(".")+1);
		agentFileMap.setFileSuffix(suffix);
		agentFileMap.setMapRule(checkRules);
		
		agentInfo.setAgentFlag(1);
		agentInfo.setAgentFileMap(agentFileMap);
		agentInfo.setAgentCofigs(agentConfigs);
		FileProcessor fileProcessor = EventProcessorFactory.getFileProcessor(agentInfo);
		List processAgentEvent = fileProcessor.processAgentEvent(checkFile, agentInfo);
		System.out.println(processAgentEvent);
	}
}
